package com.sms.utils;

import java.util.Calendar;

public class StringUtils {

    private static int[] w = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * TODO 判断字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null || str.trim().length() == 0) {
            return true;
        }
        return false;
    }

    /**
     * TODO 验证身份证合法
     *
     * @param account
     * @return
     */
    public static boolean checkUserAccount(String account) {
        if (isEmpty(account)) {
            return false;
        }
        char[] c = account.toCharArray();
        if (c.length != 18) {
            return false;
        }
        int sum = 0;
        for (int i = 0; i < w.length; i++) {
            sum += (c[i] - '0') * w[i];
        }
        char[] verifyCode = "10X98765432".toCharArray();
        char ch = verifyCode[sum % 11];
        return c[17] == ch;
    }

    public static String getId(Integer id) {
        if (id == null) {
            id = 1;
        }
        String mid = Integer.toString(id);
        if (isEmpty(mid)) {
            mid = "1";
        }
        while (mid.length() < 4) {
            mid = "0" + mid;
        }
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.get(Calendar.YEAR)) + mid;
    }

    public static String getTeacherId(String id) {
        if (!isEmpty(id)) {
            id = "1";
        }
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.get(Calendar.YEAR)) + id + 'T';
    }
}
